﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using ELMApplication.Common.Model;
using ELMApplication.Common.Const;
using System.Linq;
using Microsoft.Practices.Composite.Events;
using ELMApplication.Common.Events;
using ELMWCFServiceClass.ELMWCF;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Modularity;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;


namespace DataFlowModule.Views.DataflowView
{
    public class DataflowPrentationModel : IDataflowPresentationModel
    {
        private ELMServiceClient _domaincontext;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager regionManager;
        private readonly IModuleManager moduleManager;
        public DataflowPrentationModel(IDataflowView view, ELMServiceClient domaincontext, IEventAggregator eventAggregator, IRegionManager regionManage, IModuleManager moduleManager)
        {
            this._domaincontext = domaincontext;
            this._eventAggregator = eventAggregator;
            this.regionManager = regionManage;
            this.moduleManager = moduleManager;
            this.View = view;
            _eventAggregator.GetEvent<TaskDataEvent>().Subscribe(GetInfo, true);
            // _eventAggregator.GetEvent<DataSubmitedEvent>().Subscribe(msg, true);
            view.SetModel(this);
            //LoadInflowDetailInfos("h1-1", "h2");
        }

        private void GetInfo(TaskData data)
        {
            LoadInflowDetailInfos(data.ID, Current.User.HumanId);



        }

        #region IDataflowPresentationModel Members

        public IDataflowView View
        {
            get;
            set;

        }
        private Human _sender;
        public Human Sender
        {
            get
            {
                return _sender;
            }
            set
            {
                _sender = value;
                NotifyPropertyChanged("Sender");
            }
        }

        private Human _receiver;
        public Human Receiver
        {
            get
            {
                return _receiver;
            }
            set
            {
                _receiver = value;
                NotifyPropertyChanged("Receiver");
            }
        }
        private Dataflow _dataflow;
        public Dataflow CurrentDataflow
        {
            get
            {
                return _dataflow;
            }
            set
            {
                _dataflow = value;
                NotifyPropertyChanged("CurrentDataflow");
            }
        }


        #endregion

        private void LoadInflowDetailInfos(int tdId, string uid)
        {

            _domaincontext.GetDataflowWithAllHumanByTaskIdAndUidAsync(tdId, uid);
            _domaincontext.GetDataflowWithAllHumanByTaskIdAndUidCompleted += (sender, e) =>
                {
                    if (e.Error == null)
                    {
                        CurrentDataflow = e.Result;//赋值当前流入信息
                        Receiver = e.Result.ReceiveUser;//赋值接收人
                        Sender = CurrentDataflow.SenderUser;//赋值发送人
                        ItemMatter = CurrentDataflow.Matter;//赋值物料
                        
                        NotifyPropertyChanged("CurrentDataflow");
                        _domaincontext.GetAllDataflowDetialsAsync(CurrentDataflow.TaskID);
                        _domaincontext.GetAllDataflowDetialsCompleted += (sender1, data) =>
                            {
                                if (data.Error == null)
                                {
                                    MonitorDataList = data.Result;
                                    
                                }
                            };
                    }
                };
            
          
           


        }
     




        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region IDataflowPresentationModel Members

        private Matter _itemMatter;
        public Matter ItemMatter
        {
            get
            {
                return _itemMatter;

            }
            set
            {
                _itemMatter = value;
                NotifyPropertyChanged("ItemMatter");
            }
        }

        #endregion

        #region IDataflowPresentationModel Members

        private Dataflow _outMessage;
        public Dataflow OutMessage
        {
            get
            {
                return _outMessage;
            }
            set
            {
               
                _outMessage = value;
                NotifyPropertyChanged("OutMessage");
                
                
              

            }
        }

        public void InitialOutMessage()
        {
            if (CurrentDataflow != null)
            {
                OutMessage = new Dataflow(); 
                OutMessage.CreateDate = CurrentDataflow.CreateDate;
                OutMessage.MaxIndex = CurrentDataflow.MaxIndex + 1;
                OutMessage.SenderUser = Current.User;
                Sender = Current.User;
               
                OutMessage.ReceiveUser = null;
                Receiver = null;
                OutMessage.TaskID = CurrentDataflow.TaskID;
                OutMessage.SubTaskId = Current.User.HumanId + "-" + CurrentDataflow.CurrentOutMSGMaxIndex;
                OutMessage.StartDate = DateTime.Now;
                OutMessage.OutflowId =( int.Parse(CurrentDataflow.OutflowId) + 1).ToString();
                OutMessage.AllHuman = CurrentDataflow.AllHuman;
                NotifyPropertyChanged("OutMessage");
            }
        }

        #endregion

        #region IDataflowPresentationModel Members


        public int SendOutMessage()
        {
            int bl = 0;
            if (OutMessage != null)
            {
                if (OutMessage.ReceiveUser == null)
                {
                   // Validator.ValidateProperty(OutMessage.ReceiveUser, new ValidationContext(this,null, null) { MemberName = "ReceiveUser" });
                    MessageBox.Show("请选择接收人");
                    return 0;
                }
                _domaincontext.InsertDataflowDetailAsync(OutMessage);
                _domaincontext.InsertDataflowDetailCompleted += (sender, data) =>
                    {
                        if (data.Error == null && data.Result!=0)
                        {
                            bl = data.Result;
                            OutMessage.ID = bl;
                            NotifyPropertyChanged("OutMessage");
                            MessageBox.Show("发送成功！");
                        }
                        else
                        {
                            MessageBox.Show("消息发送失败！");
                        }
                    };
            }
            else
            {
            }
            return bl;
        }

        #endregion

        #region IDataflowPresentationModel Members

        private ObservableCollection<Dataflow> _monitorDataList;
        public ObservableCollection<Dataflow> MonitorDataList
        {
            get
            {
                return _monitorDataList;
            }
            set
            {
                _monitorDataList = value;
                NotifyPropertyChanged("MonitorDataList");
            }
        }

        #endregion

        #region IDataflowPresentationModel Members


        public void ReplyMessage()
        {

            if (CurrentDataflow != null)
            {
                
                OutMessage = new Dataflow();
                OutMessage.CreateDate = CurrentDataflow.CreateDate;
                OutMessage.MaxIndex = CurrentDataflow.MaxIndex + 1;
                OutMessage.ReceiveUser = CurrentDataflow.SenderUser;
                OutMessage.SenderUser = Current.User;
                Sender = Current.User;
                Receiver = OutMessage.ReceiveUser ;
                OutMessage.TaskID = CurrentDataflow.TaskID;
                OutMessage.SubTaskId = Current.User.HumanId + "-" + CurrentDataflow.CurrentOutMSGMaxIndex;
                OutMessage.StartDate = DateTime.Now;
                OutMessage.OutflowId = (int.Parse(CurrentDataflow.OutflowId) + 1).ToString();
                OutMessage.AllHuman = CurrentDataflow.AllHuman;
                NotifyPropertyChanged("OutMessage");
                NotifyPropertyChanged("Sender");
                NotifyPropertyChanged("Receiver");
            }
        }

        #endregion
    }
}
